<?php
class CategoryCommonAction extends CommonAction {
    public function index(){
		if(IS_POST){
			$ids = $_POST['id'];
			foreach($ids as $id=>$rowSort){
				$this->modelObject->where(array('id'=>$id))->setField('row_sort', $rowSort);
			}
			$this->success('更新成功', U('index'));
		}else{
			$this->assign('htmlList', $this->getCateHtmlList());
			$this->display();
		}
    }
	
	public function add(){
		if(IS_POST){			
			if($this->modelObject->create()){
				if($cateId =$this->modelObject->add()){
					
					/*cate_path*/
					$pid = I('pid', 0, 'intval');
					$this->setCatePath($pid, $cateId);
					
					$this->success('插入数据成功', U('index'));	
				}else{
					$this->error('插入失败!'.$this->modelObject->getError());
				}
			}else{
				$this->error('创建数据失败!'.$this->modelObject->getError());
			}
		}else{
			$this->assign('cateOption', $this->getCateOption());
			$this->display();	
		}
	}
	
	public function edit(){
		if(IS_POST){
			if($this->modelObject->create()){
				if($this->modelObject->save()){
					/*cate_path*/
					$pid = I('pid', 0, 'intval');
					$cateId = I('id', 0, 'intval');
					$this->setCatePath($pid, $cateId);
					$this->success('编辑成功', U('index'));	
				}else{
					$this->error('编辑失败!'.$this->modelObject->getError());
				}
			}else{
				$this->error('创建数据失败!'.$this->modelObject->getError());
			}
		}else{
			$id = I('id', 0, 'intval');
			if($id){
				$info = $this->modelObject->getInfo($id);
				
				$this->assign('cateOption', $this->getCateOption());
				$this->assign('info', $info);
				$this->display();	
			}else{
				$this->error('参数错误!');
			}
		}
	}
	
	public function del(){
		$id = I('id', 0, 'intval');
		if($id){
			if($this->modelObject->deleteCate($id)){
				$this->success('删除成功', U('index'));	
			}else{
				$this->error('删除失败!');
			}
		}else{
			$this->error('参数错误!');
		}
	}
	
	protected function setCatePath($pid, $cateId){
		if($pid === 0){
			$this->modelObject->where(array('id'=>$cateId))->setField('cate_path', $cateId.',');
		}else if($pid > 0){
			$paretnCateInfo = $this->modelObject->getInfo($pid);
			$this->modelObject->where(array('id'=>$cateId))->setField('cate_path', $paretnCateInfo['cate_path'].$cateId.',');	
		}	
	}
	
	
	private function getCateHtmlList(){
		$cateList = $this->modelObject->getAll(array(), 'row_sort ASC, id ASC');
		$newCateList = '';
		foreach($cateList as $key=>$val){
			if($val['pid'] == 0){
				$newCateList .= '<tbody>';
				$newCateList .= $this->buildCateHtml(1, $val);
				unset($cateList[$key]);
				$newCateList .= $this->getCateSonHtmlList($cateList, $val['id']);	
				$newCateList .= '</tbody>';
			}	
		}
		
		return $newCateList;	
	}
	private function getCateSonHtmlList($cateList, $code){
		  $newCateList = '';
		  foreach($cateList as $key=>$val){
			  if($val['pid'] == $code){
				$level = substr_count($val['cate_path'], ',');
				$newCateList .= $this->buildCateHtml($level, $val);
				unset($cateList[$key]);
				$newCateList .= $this->getCateSonHtmlList($cateList, $val['id']);	
			  }	
		  }
		  
		  return $newCateList;
	}
	private function buildCateHtml($level, $val){
		$newCateList = '';
		$newCateList .= '<tr class="level-0'.$level.'">';
		$newCateList .= '  <td><span></span><input name="id['.$val['id'].']" type="text" size="3" class="ipt" value="'.$val['row_sort'].'">&nbsp;'.$val['name'].'【'.$val['id'].'】</td>';
		
		if($val['status'] == 1){
			$newCateList .= '  <td>显示</td>';
		}else{
			$newCateList .= '  <td>不显示</td>';
		}
		
		$newCateList .= '  <td><a href="'.U('add', array('pid'=>$val['id'])).'">增加子分类</a>|<a href="'.U('edit', array('id'=>$val['id'])).'">编辑</a>|<a href="'.U('del', array('id'=>$val['id'])).'" onclick="return confirm(\'你确定要删除这个分类吗?\');">删除</a></td>';
		$newCateList .= '</tr>';
		
		return $newCateList;	
	}
	
	public function getCateOption(){
		$cateList = $this->modelObject->getAll();
		
		$newCateOption = '';
		foreach($cateList as $key=>$val){
			if($val['pid'] == 0){
				$newCateOption .= '<option value="'.$val['id'].'">'.$val['name'].'</option>';
				unset($cateList[$key]);
				$newCateOption .= $this->getCateSonOption($cateList, $val['id']);	
			}	
		}
		
		return $newCateOption;	
	}
	private function getCateSonOption($cateList, $code){
		  $newCateOption = '';
		  foreach($cateList as $key=>$val){
			  if($val['pid'] == $code){
				 $level = substr_count($val['cate_path'], ',');
				 $str = str_repeat('&nbsp;', $level).'|-';
				 $newCateOption .= '<option value="'.$val['id'].'">'.$str.$val['name'].'</option>';
				  unset($cateList[$key]);
				  $newCateOption .= $this->getCateSonOption($cateList, $val['id']);	
			  }	
		  }
		  
		  return $newCateOption;
	}
	private function getCateTree(){
		$cateList = $this->modelObject->getAll();
		
		$newCateList = array();
		foreach($cateList as $key=>$val){
			if($val['pid'] == 0){
				$newCateList[] = $cateList[$key];
				unset($cateList[$key]);
			}	
		}
		
		foreach($newCateList as $k=>$v){
			$newCateList[$k]['tree'] = $this->getCateSonTree($cateList, $v['id']);	
		}
		return $newCateList;
	}
	private function getCateSonTree($cateList, $code){
		  $newCateList = array();
		  foreach($cateList as $key=>$val){
			  if($val['pid'] == $code){
				  $newCateList[$key] = $cateList[$key];
				  unset($cateList[$key]);
				  $newCateList[$key]['tree'] = $this->getCateSonTree($cateList, $val['id']);	
			  }	
		  }
		  
		  return $newCateList;
	}
	
}//end class